// import 'package:flutter/material.dart';
// import 'package:flutter/services.dart';
//
//
// class Scene03SingleDelete extends StatefulWidget {
//   const Scene03SingleDelete({super.key});
//   @override
//   State<Scene03SingleDelete> createState() => _Scene03SingleDeleteState();
// }
//
// class _Scene03SingleDeleteState extends State<Scene03SingleDelete>
//     implements DeltaTextInputClient {
//   late final TextInputConnection _conn;
//   TextEditingValue _value = const TextEditingValue(text: 'Hello');
//   final List<String> _logs = [];
//
//   /* === DeltaTextInputClient === */
//   @override
//   TextEditingValue get currentTextEditingValue => _value;
//
//   @override
//   void updateEditingValueWithDeltas(List<TextEditingDelta> deltas) {
//     _logs.add('updateEditingValueWithDeltas');
//     for (final d in deltas) {
//       if (d is TextEditingDeltaDeletion) {
//         _logs.add('单字符删除: "${d.textDeleted}" 位置=${d.deletedRange.start}');
//       }
//     }
//     setState(() {
//       for (final d in deltas) _value = d.apply(_value);
//     });
//   }
//   /* 其余空实现，保证零报错 */
//   @override
//   void updateEditingValue(TextEditingValue value) {
//     _logs.add('updateEditingValue');
//
//   }
//   @override
//   void performAction(TextInputAction action) {
//     _logs.add('performAction');
//
//   }
//   @override
//   void connectionClosed() {
//     _logs.add('connectionClosed');
//   }
//
//   @override
//   void insertContent(KeyboardInsertedContent content) {
//     _logs.add('insertContent');
//   }
//   @override
//   void insertTextPlaceholder(Size size) {
//     _logs.add('insertTextPlaceholder');
//   }
//   @override
//   void performPrivateCommand(String action, Map<String, dynamic> data) {
//     _logs.add('performPrivateCommand');
//   }
//   @override
//   void performSelector(String selectorName) {
//     _logs.add('performSelector');
//   }
//   @override
//   void removeTextPlaceholder() {
//     _logs.add('removeTextPlaceholder');
//   }
//   @override
//   void showAutocorrectionPromptRect(int start, int end) {
//     _logs.add('showAutocorrectionPromptRect');
//   }
//   @override
//   void showToolbar() {
//     _logs.add('showToolbar');
//   }
//   @override
//   void updateFloatingCursor(RawFloatingCursorPoint point) {
//     _logs.add('updateFloatingCursor');
//   }
//
//
//   /* === 生命周期 === */
//   @override
//   void initState() {
//     super.initState();
//     _conn = TextInput.attach(
//       this,
//       const TextInputConfiguration(enableDeltaModel: true),
//     )
//       ..show()
//       ..setEditingState(_value);
//   }
//
//   @override
//   void dispose() {
//     _conn.close();
//     super.dispose();
//   }
//
//   /* === UI === */
//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       appBar: AppBar(title: const Text('单字符删除 Delta 场景')),
//       body: Column(
//         children: [
//           Expanded(
//             child: ListView.builder(
//               padding: const EdgeInsets.all(8),
//               itemCount: _logs.length,
//               itemBuilder: (_, i) => Text(_logs[i],
//                   style: const TextStyle(fontSize: 12, fontFamily: 'monospace')),
//             ),
//           ),
//           Padding(
//             padding: const EdgeInsets.all(12),
//             child: TextField(
//               controller: TextEditingController.fromValue(_value),
//               decoration: const InputDecoration(
//                 hintText: '按退格键，观察单字符删除 Delta',
//                 border: OutlineInputBorder(),
//               ),
//               onChanged: (_) {},
//             ),
//           ),
//         ],
//       ),
//     );
//   }
//
//   @override
//   // TODO: implement currentAutofillScope
//   AutofillScope? get currentAutofillScope => throw UnimplementedError();
//
//   @override
//   void didChangeInputControl(TextInputControl? oldControl, TextInputControl? newControl) {
//     _logs.add('didChangeInputControl');
//   }
// }

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';



class Scene03SingleDelete extends StatefulWidget {
  const Scene03SingleDelete({super.key});
  @override
  State<Scene03SingleDelete> createState() => _Scene03SingleDeleteState();
}

class _Scene03SingleDeleteState extends State<Scene03SingleDelete>
    implements DeltaTextInputClient {
  late final TextInputConnection _conn;
  TextEditingValue _value = const TextEditingValue(text: 'Hello Delta');
  final List<String> _logs = [];

  /* === DeltaTextInputClient 完整实现 === */
  @override
  TextEditingValue get currentTextEditingValue => _value;

  @override
  void updateEditingValueWithDeltas(List<TextEditingDelta> deltas) {
    for (final d in deltas) {
      if (d is TextEditingDeltaDeletion) {
        _logs.add(
            '删除 Delta: "${d.textDeleted}" 位置=${d.deletedRange.start}-${d.deletedRange.end}');
      }
    }
    setState(() {
      for (final d in deltas) _value = d.apply(_value);
    });
  }

  /* 其余空实现，零报错 */
  @override
  void updateEditingValue(TextEditingValue value) {}
  @override
  void performAction(TextInputAction action) {}
  @override
  void connectionClosed() {}

  @override
  void insertContent(KeyboardInsertedContent content) {}
  @override
  void insertTextPlaceholder(Size size) {}
  @override
  void performPrivateCommand(String action, Map<String, dynamic> data) {}
  @override
  void performSelector(String selectorName) {}
  @override
  void removeTextPlaceholder() {}
  @override
  void showAutocorrectionPromptRect(int start, int end) {}
  @override
  void showToolbar() {}
  @override
  void updateFloatingCursor(RawFloatingCursorPoint point) {}

  /* === 生命周期 === */
  @override
  void initState() {
    super.initState();
    _conn = TextInput.attach(
      this,
      const TextInputConfiguration(enableDeltaModel: true),
    )
      ..show()
      ..setEditingState(_value);
  }

  @override
  void dispose() {
    _conn.close();
    super.dispose();
  }

  /* === 手动触发删除 Delta === */
  void _deleteRange(int start, int end) {
    final deleted = _value.text.substring(start, end);
    final delta = TextEditingDeltaDeletion(
      oldText: _value.text,
      deletedRange: TextRange(start: start, end: end),
      selection: TextSelection(baseOffset: start,extentOffset: end),
      composing: TextRange(start: start, end: end),
      // deletedText: deleted,
    );
    updateEditingValueWithDeltas([delta]);
  }

  /* === UI === */
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Delta 删除场景')),
      body: Column(
        children: [
          Expanded(
            child: ListView.builder(
              padding: const EdgeInsets.all(8),
              itemCount: _logs.length,
              itemBuilder: (_, i) => Text(_logs[i],
                  style: const TextStyle(fontSize: 12, fontFamily: 'monospace')),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(12),
            child: TextField(
              controller: TextEditingController.fromValue(_value),
              decoration: const InputDecoration(
                hintText: 'Hello Delta',
                border: OutlineInputBorder(),
              ),
              onChanged: (_) {},
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              ElevatedButton(
                onPressed: () => _deleteRange(0, 5), // 删除 "Hello"
                child: const Text('删除 Hello'),
              ),
              ElevatedButton(
                onPressed: () => _deleteRange(6, 11), // 删除 "Delta"
                child: const Text('删除 Delta'),
              ),
            ],
          ),
        ],
      ),
    );
  }

  @override
  // TODO: implement currentAutofillScope
  AutofillScope? get currentAutofillScope => throw UnimplementedError();

  @override
  void didChangeInputControl(TextInputControl? oldControl, TextInputControl? newControl) {
    // TODO: implement didChangeInputControl
  }
}
